import net.jini.core.entry.Entry;
import net.jini.core.discovery.LookupLocator;
import net.jini.core.lookup.ServiceRegistrar;
import net.jini.core.lookup.ServiceTemplate;
import net.jini.lookup.entry.Name;

import java.rmi.RMISecurityManager;
import java.io.*;

public class BydloUser {
	
	public static SynchronizeService xxx_connect() {
		System.out.println("Looking for copy center...");
		try {
			System.out.println("System: setting security manager");
			System.setSecurityManager (new RMISecurityManager ());
	
			LookupLocator lookup = new LookupLocator ("jini://localhost");
	
			System.out.println("System: getting lookup registrar");
			ServiceRegistrar registrar  = lookup.getRegistrar();
			ServiceTemplate template = new ServiceTemplate (null, new Class[] {SynchronizeService.class}, null);
				
			System.out.println("System: starting lookup!");
			return (SynchronizeService) registrar.lookup (template);
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		} 
	}
	
	public static void xxx_menu() {
		System.out.println("*****************************************************");		
		System.out.println("*Synchronization service client by Antoshka & Valerka*");
		System.out.println("*****************************************************");
		System.out.println("Menu:");
		System.out.println("1. Print all photos");
		System.out.println("2. Copy all photos to disc");
		System.out.println("3. Bye");
		System.out.print("Your choice: ");
	}
	
	public static void xxx_1() throws java.rmi.RemoteException {
		SynchronizeService ss = xxx_connect();
		
		if (ss == null) {
			System.out.println("Copy center not found, sorry");
			return;
		} else {
			System.out.println("Copy center found, processing request");
		} 
		
		ss.print();
	}
	
	public static void xxx_2() throws java.rmi.RemoteException {
		SynchronizeService ss = xxx_connect();
		
		if (ss == null) {
			System.out.println("Copy center not found, sorry");
			return;
		} else {
			System.out.println("Copy center found, processing request");
		} 
		
		ss.download();
	}
	
	// and this topor will be made out of wood bacause nobody is going to read up to this line
    public static void main (String[] args) throws Exception { 
		BufferedReader br =	new BufferedReader(new InputStreamReader(System.in));
		
		xxx_menu();			

		while (true) {
			String s = br.readLine();
			if (s.equals("1")) {
				System.out.println("You have selected to print all photos from your camera, please wait");
				xxx_1();
				System.out.println("Done");
			} else if (s.equals("2")) {
				System.out.println("You have selected to copy all photos from your camera to disc");
				xxx_2();
				System.out.println("Done");
			} else if (s.equals("3")){
				System.out.println("Good bye!");
				break;
			} else {
				xxx_menu();
			} 
		}
    }
}
